
from nd_utils.xml_operation import *
from nd_utils.networkx_util import *

class Xml2Dict():
    def __init__(self, xml_filepath):
        self.xml_filepath = xml_filepath
        self.xml_root = get_xml_root(self.xml_filepath)

        self.personID = get_xml_ele_text( self.xml_root, 'personID' )
        self.fullName = get_xml_ele_text( self.xml_root, 'FullName' )

        self.publication_dict = self._get_publication_dict()

        self.idx_paperid_dict = self._get_idx_paperid_dict()
        self.paperidx_list = self.idx_paperid_dict.keys()

        self.paperid_idx_dict = self._get_paperid_idx_dict()
        self.paperid_list = self.paperid_idx_dict.keys()

        self.init_graph = new_graph( len(self.paperid_list) )
    """
        all publication xml to dict
        paper_id : publication attribute
    """
    def _get_publication_dict(self):
        publication_ele_list = get_xml_ele_list( self.xml_root, 'publication' )

        publication_dict = {}
        for publication_ele in publication_ele_list:
            title = get_xml_ele_text( publication_ele, 'title')
            year = get_xml_ele_text( publication_ele, 'year')
            authors = get_xml_ele_text( publication_ele, 'authors')
            jconf = get_xml_ele_text( publication_ele, 'jconf')
            paper_id = get_xml_ele_text( publication_ele, 'id')
            label = get_xml_ele_text( publication_ele, 'label')
            organization = get_xml_ele_text( publication_ele, 'organization')

            publication_dict[paper_id] = {}
            publication_dict[paper_id]['title'] = title
            publication_dict[paper_id]['year'] = year
            publication_dict[paper_id]['authors'] = authors
            publication_dict[paper_id]['jconf'] = jconf
            publication_dict[paper_id]['id'] = paper_id
            publication_dict[paper_id]['label'] = label
            publication_dict[paper_id]['organization'] = organization

        return publication_dict

    """
        idx : paperid
    """
    def _get_idx_paperid_dict(self):
        paperid_list = self.publication_dict.keys()
        idx_paperid_dict = {}
        for idx, paperid in enumerate(paperid_list):
            idx_paperid_dict[idx] = paperid
        return idx_paperid_dict
    """
        paperid : idx
    """
    def _get_paperid_idx_dict(self):
        paperid_list = self.publication_dict.keys()
        paperid_idx_dict = {}
        for idx, paperid in enumerate(paperid_list):
            paperid_idx_dict[paperid] = idx
        return paperid_idx_dict



